﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Security.Cryptography;
using System.Web.UI;

namespace Common.Util
{
    /// <summary>
    /// Helper 的摘要说明
    /// </summary>
    public class JSHelper
    {
        public JSHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        public static void Alert(Page page, string msg)
        {
            if (msg.IndexOf('\'') >= 0)
            {
                msg = msg.Replace("\'", "\\\'");
            }
            if (msg.IndexOf("\"") >= 0)
            {
                msg = msg.Replace("\"", "\\\"");
            }
            page.ClientScript.RegisterStartupScript(page.GetType(), "", "<script>alert('" + msg + "');</script>");
        }

        /// <summary>
        /// 说明：弹出警告窗口，并刷新页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        /// <param name="freshPage"></param>
        public static void AlertAndFreshPage(Page page, string msg)
        {
            if (msg.IndexOf('\'') >= 0)
            {
                msg = msg.Replace("\'", "\\\'");
            }
            if (msg.IndexOf("\"") >= 0)
            {
                msg = msg.Replace("\"", "\\\"");
            }
            page.ClientScript.RegisterStartupScript(page.GetType(), "", "<script>alert('" + msg + "');window.location.href=window.location.href;</script>");
        }




        /// <summary>
        /// 调用javascript方法
        /// </summary>
        /// <param name="APage"></param>
        /// <param name="funName">方法名</param>
        /// <param name="APath">字符串参数</param>
        public static void RunClientJS(Page APage, string funName, string AValue)
        {
            ClientScriptManager csm = APage.ClientScript;
            csm.RegisterStartupScript(APage.GetType(), "PopupScript", funName + "(" + AValue + ");", true);

        }


        /// <summary>
        ///  /// 弹出消息框并且转向到新的URL         
        /// </summary>
        /// <param name="page">Web窗体页</param>
        /// <param name="message">消息内容</param>
        /// <param name="toURL">连接地址</param>
        public static void AlertThenRedirect(Page page, string message, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');window.location.href='{1}';</script>";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "AlertThenRedirect", string.Format(js, message, toURL));
            }
        }

        public static void Redirect(Page page, string url)
        {
            string js = "<script language=javascript>window.location.href='{0}';</script>";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "AlertThenRedirect", string.Format(js, url));
            }
        }

    }
}